package com.richesup.common.oss.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author yuhong
 * @date 2018-06-27 10:30
 * @description
 */
public class File {

    private final static Logger logger= LogManager.getLogger(File.class);

    public static Boolean CreateFolder(String path){
        try {

            java.io.File file =new java.io.File(path);
//如果文件夹不存在则创建
            if  (!file.exists())
            {
                file.mkdirs();
                //Files.createDirectory(Paths.get(path));
            }

        }catch(Exception e){
            logger.error(e.toString());
            return false;
        }
        logger.debug("CreateFolder Success："+path);
        return true;
    }

    /**
     *
     * 功能描述:保存文件
     *
     * @param: [multipartFile, path]
     * @return: 文件名称
     * @auther: yuhong
     * @date: 2018-6-27 13:41
     */
    public static String SaveFile(MultipartFile multipartFile, String path){

        //验证
        //1、需要有数据
        //2、后辍只能是png、jpg、gif

        if (multipartFile.isEmpty()){
            logger.error("multipartFile Empty()");
            return null;
        }

        //创建目录
        CreateFolder(path);

        String strPrefix=multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);
        String fileName = new  StringBuilder().append(Common.GetUUID()).append(".").append(strPrefix).toString();

        BufferedOutputStream bos=null;
        FileInputStream fileInputStream=null;
        Boolean blnIsSuccess=true;
        try {
            fileInputStream = (FileInputStream) multipartFile.getInputStream();

            bos = new BufferedOutputStream(new FileOutputStream(path + java.io.File.separator + fileName));
            byte[] bs = new byte[1024];
            int len;
            while ((len = fileInputStream.read(bs)) != -1) {
                bos.write(bs, 0, len);
            }
            bos.flush();
            bos.close();
        }catch (Exception e){
            logger.error("BufferedOutputStream error："+e.getStackTrace().toString());
            blnIsSuccess=false;
        }finally {
            if (bos!=null) {
                try {
                    bos.close();
                    bos=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                    fileInputStream=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        if (!blnIsSuccess){
            return null;
        }

        logger.debug("SaveFile SUCCESS");
        return fileName;
    }
}
